<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Wishlist extends MX_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    function Wishlist() {
        parent::__construct();
        $this->load->model('helpermodel');
    }

    public function index() {
        $this->lang->load('wishlist/wishlist', $this->config->item('language'));
        //SEO
        $data['title'] = $this->lang->line('wishlist_text_title');
        $data['description'] = $this->lang->line('wishlist_text_description');
        $data['keywords'] = $this->lang->line('wishlist_text_keywords');
        
        $data['module'] = "wishlist";
        $data['view_file'] = "wishlist";

        if ($this->input->get('remove')) {
            $key_remove = array_search($this->input->get('remove'), $this->session->userdata('wishlist'));

            $wishlist = $this->session->userdata('wishlist');
            unset($wishlist[$key_remove]);
            $this->session->set_userdata('wishlist', $wishlist);

            $this->session->set_userdata('success', 'Success: You have modified your wishlist!');

            redirect(base_url() . 'shortlist/');
        }

        if ($this->session->userdata('success')) {
            $data['success'] = $this->session->userdata('success');

            $this->session->unset_userdata('success');
        } else {
            $data['success'] = FALSE;
        }

        $this->load->model('wishlist_model');

        $options['country'] = NULL;
        $options['list_type'] = NULL;
        $options['propertyfor'] = NULL;
        $options['propertytype'] = NULL;
        $options['cities'] = NULL;
        $options['locations'] = NULL;
        $options['subarea'] = NULL;
        $options['price_min'] = NULL;
        $options['price_max'] = NULL;
        $options['size_min'] = NULL;
        $options['size_max'] = NULL;
        $options['bedrooms'] = NULL;
        $options['bathrooms'] = NULL;
        $options['keyword'] = NULL;
        $options['features'] = NULL;
        $options['facilities'] = NULL;
        $limit['offset'] = 0;
        $limit['limit'] = 999999;
        $options['orderby'] = NULL;
        $options['language'] = 1;

        $data['listings'] = array();

        if ($this->session->userdata('wishlist')) {
            foreach ($this->session->userdata('wishlist') as $key => $listingid) {
                $listing_info = $this->wishlist_model->getListingWithWhere($options, $limit, $listingid);
                if (isset($listing_info)) {
                    $listing_info = $listing_info[0];

                    $data['listings'][] = array(
                        'listingid' => $listing_info['Listing_ID'],
                        'name' => $listing_info['ProjectName_1'],
                        'price' => $listing_info['AskingPriceLocal'],
                        'href' => base_url() . 'detail.html?id=' . $listing_info['Listing_ID'],
                        'remove' => base_url() . 'shortlist.html?remove=' . $listing_info['Listing_ID']
                    );
                } else {
                    $this->session->unset_userdata($key);
                }
            }
        }

        echo Modules::run('template/default_template', $data);
    }

    public function add() {
        $json = array();
        $options = array();

        $wishlist = $this->session->userdata('wishlist');
        if (!$wishlist) {
            $wishlist = array();
        }

        $listingid = $this->input->post('listingid');
        if (!isset($listingid)) {
            $listingid = 0;
        }

        $this->load->model('wishlist_model');

        $options['country'] = NULL;
        $options['list_type'] = NULL;
        $options['propertyfor'] = NULL;
        $options['propertytype'] = NULL;
        $options['cities'] = NULL;
        $options['locations'] = NULL;
        $options['subarea'] = NULL;
        $options['price_min'] = NULL;
        $options['price_max'] = NULL;
        $options['size_min'] = NULL;
        $options['size_max'] = NULL;
        $options['bedrooms'] = NULL;
        $options['bathrooms'] = NULL;
        $options['keyword'] = NULL;
        $options['features'] = NULL;
        $options['facilities'] = NULL;
        $limit['offset'] = 0;
        $limit['limit'] = 999999;
        $options['orderby'] = NULL;
        $options['language'] = 1;

        $listing_info = $this->wishlist_model->getListingWithWhere($options, $limit, $listingid);
        if (isset($listing_info) && !is_null($listing_info)) {
            $listing_info = $listing_info[0];
        }

        if ($listing_info) {
            if (!in_array($this->input->post('listingid'), $wishlist)) {
                array_push($wishlist, $this->input->post('listingid'));
                $this->session->set_userdata(array("wishlist" => $wishlist));
            }

            $json['success'] = sprintf('Success: You have added <a href="%s">%s</a> to your <a href="%s">shortlist</a>!', base_url() . 'detail.html?id=' . $this->input->post('listingid'), $listing_info['ProjectName_1'], base_url() . 'shortlist/');

            $json['total'] = sprintf('Wish List (%s)', (isset($wishlist) ? count($wishlist) : 0));
        }

        echo json_encode($json);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */